package com.wokeMy.view;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.addressactivity.UpgradeMemberShipActivity;
import com.woke.method.MyApp;
import com.woke.serizedatas.Datas_load;
import com.wokeMy.view.adpter.MyLeftAdapter;
import com.wokeMy.view.adpter.MyRightAdapter;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.model.RightModel;
import com.wokeMy.view.util.Util;
import com.wokeMy.view.util.ViewTools;
import com.wokeMy.view.view.SyncHorizontalScrollView;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class PassFlActivity extends BaseActivity {



    private TextView vipup_pass_fl;
    private ImageView back;



    private LinearLayout leftContainerView;
    private ListView leftListView;
    private List<String> leftlList;
    private LinearLayout rightContainerView;
    private ListView rightListView;
    private List<RightModel> models;
    private SyncHorizontalScrollView titleHorsv;
    private SyncHorizontalScrollView contentHorsv;

    Dialog dialog;

    MyLeftAdapter adapter;
    MyRightAdapter myRightAdapter;

    private Datas_load loaded;
    String type;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_pass_fl);
    }

    @Override
    public void initView() {
        leftContainerView = (LinearLayout) findViewById(R.id.left_container);
        leftListView = (ListView) findViewById(R.id.left_container_listview);
        rightContainerView = (LinearLayout) findViewById(R.id.right_container);
        rightListView = (ListView) findViewById(R.id.right_container_listview);
        titleHorsv = (SyncHorizontalScrollView) findViewById(R.id.title_horsv);
        contentHorsv = (SyncHorizontalScrollView) findViewById(R.id.content_horsv);

        vipup_pass_fl= myFindViewById(R.id.vipup_pass_fl);
        back=myFindViewById(R.id.back_passlv_iv);
    }

    @Override
    public void initData() {
//        initRightData();
//        initRightData();

        loaded=((MyApp)getApplication()).getDatas_load();
        type=loaded.user_type;
        models=new ArrayList<>();
        leftlList=new ArrayList<>();
        dialog=Util.createLoadingDialog(this);
        getPassFlTable();

    }

    @Override
    public void initActivity() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        vipup_pass_fl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String user_type=getResources().getString(R.string.usertype);
                if (type.equals(user_type)){
                    Toast.makeText(PassFlActivity.this,"您已经是"+getResources().getString(R.string.usertypeString),Toast.LENGTH_SHORT).show();
                }else {
                    //表示有可升级列表
                    startActivity(new Intent(PassFlActivity.this, UpgradeMemberShipActivity.class));
                }
            }
        });

}


    //通道费率表
    private void getPassFlTable() {
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        Log.e("获取通道费率表params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.GETVIPRATE, params, new AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Util.closeDialog(dialog);
                //判断状态码
                if(i == 200){
                    //获取结果
                    try {
                        String result = new String(bytes, "utf-8");
                        //                        Toast.makeText(MobaoczActivity.this, result, Toast.LENGTH_SHORT).show();
                        Log.e("获取通道费率表result", result);
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            JSONObject jsonObject1 = jsonObject.getJSONObject("data").getJSONObject("data");
                            models=new ArrayList<RightModel>();
                            RightModel rightModel;
                            //通道标题
                            Iterator iterator = jsonObject1.keys();
                            while(iterator.hasNext()){
                                String key = (String) iterator.next();
                                leftlList.add(key);
                                //通道费率
                                JSONArray jsonArray=jsonObject1.getJSONArray(key);
                                ArrayList<String> ratedataPj=new ArrayList<>();
                                ArrayList<String> ratedata=new ArrayList<>();
                                ArrayList<String> feedata=new ArrayList<>();
                                for (int i1 = 0; i1 < jsonArray.length(); i1++) {
                                    JSONObject jsonObject2 = jsonArray.getJSONObject(i1);
                                    String rate=jsonObject2.getString("rate");
                                    rate=rate.substring(4,6);
                                    ratedata.add(rate+"%");
                                    String fee=jsonObject2.getString("fee");
                                    feedata.add(fee);
                                }
                                Log.e("ratedata1",""+ratedata.toString());
//                                //费率排序
//                                ArrayList<Integer> integers=new ArrayList<Integer>();
//                                for (int i2 = 0; i2 < ratedata.size(); i2++) {
//                                    integers.add(Integer.valueOf(ratedata.get(i2))) ;
//                                }
//                                Log.e("integers转Integer",""+integers.toString());
//
//                                Collections.sort(integers, new Comparator<Integer>() {
//                                    public int compare(Integer o1, Integer o2) {
//                                        return o2 - o1;
//                                    }
//                                });
//                                Log.e("integers降序",""+integers.toString());
//                                ratedata.clear();
                                for (int i1 = 0; i1 < ratedata.size(); i1++) {
                                    ratedataPj.add("0."+ratedata.get(i1)+"+"+feedata.get(i1));
                                }
                                Log.e("ratedataPj",""+ratedataPj.toString());
                                rightModel=new RightModel(ratedataPj.get(0),ratedataPj.get(1),ratedataPj.get(2),ratedataPj.get(3),ratedataPj.get(4));
                                models.add(rightModel);
                            }
                            // 设置两个水平控件的联动
                            titleHorsv.setScrollView(contentHorsv);
                            contentHorsv.setScrollView(titleHorsv);
                            // 添加左边内容数据
                            leftContainerView.setBackgroundColor(Color.WHITE);
                            adapter=new MyLeftAdapter(PassFlActivity.this, leftlList);
                            leftListView.setAdapter(adapter);
                            ViewTools.setListViewHeightBasedOnChildren(leftListView);
                            // 添加右边内容数据
                            rightContainerView.setBackgroundColor(Color.WHITE);
                            Log.e("type","type"+type);
                            myRightAdapter=new MyRightAdapter(PassFlActivity.this, models,type);
                            rightListView.setAdapter(myRightAdapter);
                            ViewTools.setListViewHeightBasedOnChildren(rightListView);

                            Log.e("leftadat",""+leftlList.toString());
                            Log.e("models",""+models.toString());
        // TODO: 2018/1/9 0009
//                            //通道费率
//                            for (int i2 = 0; i2 < jsonArray.length(); i2++) {
//                                String title = jsonArray.getJSONObject(i2).getString("title");
//                                for (int i3 = 0; i3 < passdata.size(); i3++) {
//                                    String passdatatitle = passdata.get(i3);
//                                    if (passdatatitle.equals(title)){
//                                        String rate=jsonArray.getJSONObject(i2).getString("rate");
//                                        rate=String.valueOf(Float.parseFloat(rate)*100);
//                                        String fee=jsonArray.getJSONObject(i2).getString("fee");
//                                        ratedata.add(rate+"+"+fee);
//                                    }
//                                }
//                            }
//                            //费率排序
//                            ArrayList<Integer> integers=new ArrayList<Integer>();
//                            for (int i1 = 0; i1 < ratedata.size(); i1++) {
//                                integers.add(Integer.parseInt(ratedata.get(i1))) ;
//                            }
//                            Collections.sort(integers, new Comparator<Integer>() {
//                                public int compare(Integer o1, Integer o2) {
//                                    return o2 - o1;
//                                }
//                            });
//                            for (int i1 = 0; i1 < integers.size(); i1++) {
//                                ratedata.add(String.valueOf(integers.get(i1))) ;
//                            }


                            if (jsonObject.get("flag").equals("success")) {

                            } else {

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });
    }

    private void initRightData() {
        models=new ArrayList<RightModel>();
        models.add(new RightModel("111", "222", "333", "444", "555"));
        models.add(new RightModel("111", "222", "333", "444", "555"));
        models.add(new RightModel("111", "222", "333", "444", "555"));
        models.add(new RightModel("111", "222", "333", "444", "555"));
        models.add(new RightModel("111", "222", "333", "444", "555"));
        models.add(new RightModel("111", "222", "333", "444", "555"));
        models.add(new RightModel("111", "222", "333", "444", "555"));
        models.add(new RightModel("111", "222", "333", "444", "555"));
        models.add(new RightModel("111", "222", "333", "444", "555"));
        models.add(new RightModel("111", "222", "333", "444", "555"));
        models.add(new RightModel("111", "222", "333", "444", "555"));
        models.add(new RightModel("111", "222", "333", "444", "555"));
        models.add(new RightModel("111", "222", "333", "444", "555"));
        models.add(new RightModel("111", "222", "333", "444", "555"));
        models.add(new RightModel("111", "222", "333", "444", "555"));

    }

    private void initLeftData() {
        leftlList=new ArrayList<String>();
        leftlList.add("aaaa");
        leftlList.add("aaaa");
        leftlList.add("aaaa");
        leftlList.add("aaaa");
        leftlList.add("aaaa");
        leftlList.add("aaaa");
        leftlList.add("aaaa");
        leftlList.add("aaaa");
        leftlList.add("aaaa");
        leftlList.add("aaaa");
        leftlList.add("aaaa");
        leftlList.add("aaaa");
        leftlList.add("aaaa");
        leftlList.add("aaaa");
        leftlList.add("aaaa");
    }
}
